<template>
  <div class="app-registry__step-one">
    <div class="app-registry__step-one__content">
      <input type="text" placeholder="输入手机号" v-model="tel">
      <button :disabled="disabled" @click="checkPhone">下一步</button>
    </div>
  </div>
</template>
<script>
import { postCheckPhoneApi } from '@/api/haigou'
import Vue from 'vue'
import { Dialog } from 'vant'

// 全局注册
Vue.use(Dialog)

export default {
  name: 'AppRegistryStepOne',
  data () {
    return {
      tel: ''
    }
  },
  computed: {
    disabled () {
      return !/^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/.test(this.tel)
    }
  },
  methods: {
    async checkPhone () {
      try {
        await postCheckPhoneApi(this.tel)
        Dialog.confirm({
          message: '我们将发送短信验证码至 <br />' + this.tel
        }).then(() => {
          // on confirm
          this.$router.push({ name: 'steptwo', params: { tel: this.tel } })
        }).catch(() => {
          // on cancel
        })
      } catch (error) {
        if (error.message === '10005') {
          Dialog.confirm({
            message: '该手机已被注册,是否跳转登录?'
          }).then(() => {
            this.$router.replace({ name: 'login' })
          }).catch(() => {
          })
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.app-registry__step-one {
  &__content {
    padding: 0.16rem .35rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    input {
      width: 100%;
      height: 50px;
      margin: 20px 0;
      border: none;
      background: rgb(240, 240, 240);
      border-radius: 15px;
      padding: 0 20px;
      margin-top: 0;
    }
    button {
      width: 100%;
      height: 50px;
      border-radius: .4rem;
      border: none;
      background: rgb(84, 167, 250);
      font-size: 18px;
      color: white;
    }
    button:disabled {
      background: rgb(181, 218, 255);
    }
  }
}
</style>
